package dp.gr03.payroll.classifications;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import dp.gr03.payroll.Date;
import dp.gr03.payroll.Paycheck;
import dp.gr03.payroll.payrollDomain.PaymentClassification;

public class HourlyClassification implements PaymentClassification
{
    protected double hourlyRate;
    protected ArrayList<TimeCard> timecards;

    public HourlyClassification(double hourlyRate)
    {
        this.hourlyRate = hourlyRate;
        timecards = new ArrayList<TimeCard>();
    }

    public double getHourlyRate()
    {
        return hourlyRate;
    }

    public TimeCard getTimeCard(GregorianCalendar date)
    {
        for (TimeCard t : timecards)
        {
            if (t.getDate().equals(date))
                return t;
        }

        return null;
    }

    public void addTimeCard(TimeCard timeCard)
    {
        timecards.add(timeCard);
    }

    @Override
    public double calculatePay(Paycheck pc)
    {
        double totalPay = 0;

        for (TimeCard tc : timecards)
        {
            if (Date.isInPayPeriod(tc.getDate(), pc))
                totalPay += calculatePayForTimeCard(tc);
        }
        return totalPay;
    }

    private double calculatePayForTimeCard(TimeCard tc)
    {
        double hours = tc.getHours();
        double overTime = Math.max(0.0, hours - 8.0);
        double straightTime = hours - overTime;
        return (straightTime * hourlyRate) + ((overTime * hourlyRate) * 1.5);
    }
}
